package com.jhhc.StormSurgeForecast.AstronomicalTide.Controller;

import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_ST_ASTROTD_F;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.DTO.Param;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.DTO.UploadWindSiteDto;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.DTO.WindSiteParam;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.ModelInfo.Lvl;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_ST_ASTROTD_FService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.WindService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Util.ModelFileRead;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Util.TimeUtil;
import com.jhhc.StormSurgeForecast.StormSurge.Service.BDMS_FBC_ASTROTD_AService;
import com.jhhc.StormSurgeForecast.StormSurge.Service.BDMS_FBC_RESULT_AService;
import com.jhhc.Weathers.datasupplement.util.NC_util;
import com.jhhc.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
 * @author LYQ
 */
@Api(value="TideInsertController",tags={"潮位数据保存服务"})
@RequestMapping(value = "StormSurgeForecast/AstronomicalTide")
@RestController
@RequiredArgsConstructor
public class InsertController {

    private final BDMS_FBC_ASTROTD_AService bdmsFbcAstrotdAService;

//    private final FileService fileService;
    private final WindService windService;

    private final BDMS_FBC_RESULT_AService bdms_fbc_result_aService;

    private final BDMS_ST_ASTROTD_FService bdms_st_astrotd_fService;
    @ApiOperation("二级网格数据入库")
    @RequestMapping(value = "Report/secondaryGridInsert", headers = "Accept=application/json", method = RequestMethod.POST)
    public JsonResult secondaryGridInsert(@RequestBody Param param){
        //return
        JsonResult jsonResult = new JsonResult();
        int count = bdms_fbc_result_aService.batchInsert(param.getPath(),param.getTime(),
                param.getCalSchemeId(),param.getAreaName());
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(count);
        return jsonResult;
    }

    /**
     * 天文潮数据入库，lvl格式
     * path 文件路径
     */
    @ApiOperation("天文潮数据入库LVL格式")
    @PostMapping(value = "Report/InsertAstronomicalLVL")
    public JsonResult insertAstronomicalLvl(@RequestParam("multipartFile") MultipartFile multipartFile,
                                            @RequestParam("unitName") int unitName){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        if (multipartFile.isEmpty()) {
            jsonResult.setRemark("文件为空");
            return jsonResult;
        }
        ModelFileRead modelFileRead = new ModelFileRead();
        TimeUtil timeUtil = new TimeUtil();
        Lvl lvl = modelFileRead.read(multipartFile,unitName);
        List<BDMS_ST_ASTROTD_F> fList = new ArrayList<>();
        List<BDMS_ST_ASTROTD_F> fs = new ArrayList<>();
        BDMS_ST_ASTROTD_F f;
        int day;
        int qDay = timeUtil.currentDay(lvl.getEigenvalue().get(0).getYmdh());
        int min;
        for (int i = 0; i < lvl.getEigenvalue().size(); i++) {
            min = timeUtil.currentMin(lvl.getEigenvalue().get(i).getYmdh());
            if (min == 0) {
                String time = timeUtil.dateAddMin(lvl.getEigenvalue().get(i).getYmdh(),1);
                lvl.getEigenvalue().get(i).setYmdh(time);
            }
            day = timeUtil.currentDay(lvl.getEigenvalue().get(i).getYmdh());
            if (day == qDay) {
                f = lvl.getEigenvalue().get(i);
                fs.add(f);
            }else {
                fList.addAll(modelFileRead.estimate(fs));
                fs.clear();
                qDay = day;
                f = lvl.getEigenvalue().get(i);
                fs.add(f);
            }
        }
        fList.addAll(modelFileRead.estimate(fs));
        fs.clear();
        lvl.setEigenvalue(fList);
        System.out.println("开始删除");
        int year = timeUtil.currentYear(lvl.getAstronomicalTide().get(0).getYmdh());
        int count = bdms_st_astrotd_fService.delete(year, lvl.getStationCodes());
        System.out.println("删除完成:" + count);
        System.out.println("开始入库");
        int s1 = bdms_st_astrotd_fService.insertBatch(lvl.getAstronomicalTide());
        int s2 = bdms_st_astrotd_fService.insertBatch(lvl.getEigenvalue());
        System.out.println("入库完成：" + (s2 + s1) + "，删除：" + count);
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData((s2 + s1));
        return jsonResult;
    }

    /**
     * path  文件路径
     * calSchemeId  计算方案id
     * 读fort.63,fort.64入BDMS_FBC_ASTROTD_A表接口
     */
    @ApiOperation("天文潮数据入库")
    @GetMapping(value = "Report/astronomicalTideInsert", headers = "Accept=application/json")
    public JsonResult astronomicalTideInsert(@RequestParam(value = "path") String path, @RequestParam(value = "pid") String pid){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        int count = bdmsFbcAstrotdAService.astronomicalTideInsert(path, pid);
        if (count == -1) {
            jsonResult.setData("入库失败");
        }else {
            jsonResult.setData(count + 1);
        }
        return jsonResult;
    }
    /**
     * path  文件路径
     * calSchemeId  计算方案id
     * 读fort.63,fort.64入BDMS_FBC_ASTROTD_A表接口
     */
    @ApiOperation("天文潮数据入库")
    @GetMapping(value = "Report/astronomicalTideInsertGrid", headers = "Accept=application/json")
    public JsonResult astronomicalTideInsertGrid(@RequestParam(value = "path") String path){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        int count = bdmsFbcAstrotdAService.astronomicalTideInsertGrid(path);
        if (count == -1) {
            jsonResult.setData("入库失败");
        }else {
            jsonResult.setData(count + 1);
        }
        return jsonResult;
    }

    /**
     * path  文件路径
     * calSchemeId  计算方案id
     * 读fort.63,fort.64入BDMS_ST_ASTROTD_A表接口
     */
    @ApiOperation("温州格点天文潮入库")
    @GetMapping(value = "Report/wenzhouGrid", headers = "Accept=application/json")
    public JsonResult astronomicalTideInsertwz(@RequestParam(value = "startTime") String startTime,
                                               @RequestParam(value = "endTime") String endTime){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        int count = bdms_st_astrotd_fService.insertBatchWz(startTime, endTime);
        if (count == 0) {
            jsonResult.setData("入库失败");
        }else {
            jsonResult.setData(count + 1);
        }
        return jsonResult;
    }


    @ApiOperation("温州天文潮数据处理")
    @GetMapping(value = "Report/WenZhouDataHandle", headers = "Accept=application/json")
    public JsonResult wenZhouDataHandle(@RequestParam(value = "year") int year){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        int count = bdms_st_astrotd_fService.wenZhouDataHandle(year);
        if (count == 0) {
            jsonResult.setData("入库失败");
        }else {
            jsonResult.setData(count + 1);
        }
        return jsonResult;
    }


    /**
     *生成风场文件
     * localFile 本地文件路径目录，自动获取最新的气象文件
     * region 区域范围
     * windSiteFile 生成风场文件的目录
     */
//    @ApiOperation("生成风场文件")
//    @RequestMapping(value = "Data/CreateWindSite", headers = "Accept=application/json", method = RequestMethod.POST)
//    public JsonResult createWindSite(@RequestBody UploadWindSiteDto param) {
//        JsonResult jsonResult = new JsonResult();
//        windService.uploadWindSite(param);
//        return jsonResult;
//    }

    /**
     *生成风场文件
     * localFile 本地文件路径目录，自动获取最新的气象文件
     * region 区域范围
     * windSiteFile 生成风场文件的目录
     */
    @ApiOperation("生成风场文件2")
    @PostMapping(value = "Data/CreateWindSite2", headers = "Accept=application/json")
    public JsonResult createWindSite2(@RequestBody WindSiteParam param) {
        JsonResult jsonResult = new JsonResult();
        long startTime=System.currentTimeMillis();
        jsonResult.setData(windService.createWindSite(param));
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        long endTime=System.currentTimeMillis();
        float excTime=(float)(endTime-startTime)/1000;
        System.out.println("执行时间："+excTime+"s");
        return jsonResult;
    }
    /**
     *生成风场文件经纬度
     * localFile 本地文件路径目录，自动获取最新的气象文件
     * region 区域范围
     * windSiteFile 生成风场文件的目录
     */
    @ApiOperation("生成风场文件经纬度")
    @GetMapping(value = "Data/CreateWindSiteXY", headers = "Accept=application/json")
    public JsonResult createWindSiteXY(@RequestParam String path,@RequestParam String scPath,@RequestParam String region) {
        JsonResult jsonResult = new JsonResult();
        long startTime=System.currentTimeMillis();
        jsonResult.setData(windService.createWindSiteXY(path, scPath, region));
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        long endTime=System.currentTimeMillis();
        float excTime=(float)(endTime-startTime)/1000;
        System.out.println("执行时间："+excTime+"s");
        return jsonResult;
    }

    /**
     * path  文件路径
     * calSchemeId  计算方案id
     * areaName 区域名称
     * time 预报开始时间
     * 读fort.63,fort.64入BDMS_FBC_RESULT_A表接口
     */
//    @ApiOperation("预报结果入库fort.63、fort.64")
//    @RequestMapping(value = "Report/insertCalResultByCSDomain", headers = "Accept=application/json", method = RequestMethod.POST)
//    public JsonResult insertCalResultByCSDomain(@RequestBody Param param) throws Exception {
//        //return
//        long startTime=System.currentTimeMillis();
//        JsonResult jsonResult = new JsonResult();
//        String path63 = param.getPath() + File.separator + param.getCalSchemeId() + File.separator + "fort.63";
//        String path64 = param.getPath() + File.separator + param.getCalSchemeId() + File.separator + "fort.64";
//        int count = 0;
//        int num = 0;
//        for (int i = 0; i < 2; i++) {
//            if (i == 1) {
//                count = bdms_fbc_result_aService.insertCalResultByCSDomain(path63,param.getTime(),
//                        param.getCalSchemeId(),param.getAreaName());
//                num += count;
//            }else {
//                count = bdms_fbc_result_aService.insertCalResultByCSDomain(path64,param.getTime(),
//                        param.getCalSchemeId(),param.getAreaName());
//                num += count;
//            }
//        }
//        if (count == -1) {
//            jsonResult.setData("入库失败");
//        }else {
//            jsonResult.setData("入库成功：" + num);
//        }
//        jsonResult.setStatusCode(200);
//        jsonResult.setSucceeded(true);
//        long endTime=System.currentTimeMillis();
//        float excTime=(float)(endTime-startTime)/1000;
//        System.out.println("执行时间："+excTime+"s");
//        return jsonResult;
//    }

}
